----------------------------------------------------------------------------------------------------------------------------------
--[[

    高自由度  真伤模块

    操作：

    loramia_com_real_damage:AddRealDamageFn(prefab,fn)

    针对特定目标执行特定伤害函数。

    格式：
    fn = function(player,target,damage)
        
    end

]]--
----------------------------------------------------------------------------------------------------------------------------------
-- 全局变量,通用库
    local all_real_dmg_fns = {}
    local default_dmg_fn = function(player,target,damage)
        damage = math.abs(damage)
        target.components.health:DoDelta(-damage,nil, nil, nil, nil, true)
    end
    local function get_damage_fn(prefab)
        if all_real_dmg_fns[prefab] then
            return all_real_dmg_fns[prefab]
        end
        return default_dmg_fn
    end
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_real_damage = Class(function(self, inst)
    self.inst = inst
    self.over_written_reald_dmg_fns = {}    --- 覆盖最高级的的真伤函数    
end,
nil,
{

})

------------------------------------------------------------------------------------------------------------------------------
--- 配置函数
    function loramia_com_real_damage:GetRealDamageFn(prefab)
        return self.over_written_reald_dmg_fns[prefab] or get_damage_fn(prefab)
    end
    function loramia_com_real_damage:AddRealDamageFn(prefab,fn) --- 添加一个真伤函数，通用库
        all_real_dmg_fns[prefab] = fn
    end
    function loramia_com_real_damage:AddOverWrittenRealDamageFn(prefab,fn) -- 覆盖最高级的真伤函数
        self.over_written_reald_dmg_fns[prefab] = fn
    end
------------------------------------------------------------------------------------------------------------------------------
--- 造成伤害
    function loramia_com_real_damage:DoRealDamage(target,damage)
        damage = math.abs(damage)
        if damage == 0 or target.components.health == nil then
            print("error in loramia_com_real_damage : target health is nil")
            return
        end
        local prefab = target.prefab
        local damage_fn = self:GetRealDamageFn(prefab)

        local target_current_health = target.components.health.currenthealth
        -- target.components.health:DoDelta(-damage,nil, nil, nil, nil, true)
        -- damage_fn(self.inst,target,damage)
        local crash_flag,crash_reason = pcall(damage_fn,self.inst,target,damage)
        if not crash_flag then
            print("error in loramia_com_real_damage : crash",self.inst,target,damage)
            print("+++",crash_reason)
            return
        end
        local new_health = target.components.health.currenthealth
        if new_health < target_current_health and new_health <= 0 then
            self.inst:PushEvent("killed", { victim = target, attacker = self.inst })
        else
            target:PushEvent("attacked", { attacker = self.inst, damage = damage,})
        end
        -- print("loramia_com_real_damage : ",target,damage)
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_real_damage







